package com.dao.impl;

import com.dao.CollectService;
import com.entity.Collect;
import com.entity.Comment;
import com.entity.Member;
import com.entity.Songer;
import com.util.JdbcUtil;
import com.util.PageUtil;

import javax.sql.rowset.JdbcRowSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 收藏 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2022-03-23
 */
public class CollectServiceImpl  implements CollectService {

    @Override
    public int deletedIds(String[] ids) throws SQLException {
        for (int i = 0; i < ids.length; i++) {
            System.out.println(ids[i]);
        }
        String sql = "update collect set is_deleted = 1,update_time = ? where collect_id in (0";

        List<Object> list = new ArrayList<>();
        list.add(new Date());
        for (int i = 0; i < ids.length; i++) {
            sql += ",?";

            list.add(ids[i]);
        }

        sql += ")";

        int i = JdbcUtil.insertOrUpdateOrDelete(sql, list.toArray());

        return i;
//        String sql = "update collect set is_deleted = 1 ,update_time = ? where member_id in (";
//        for (int i = 0; i < obj.size();i++) {
//            sql += "?,";
//        }
//        String substring = sql.substring(0,sql.length()-1);
//        substring = substring + ")";
//        obj.add(0,new Date());
//        return JdbcUtil.insertOrUpdateOrDelete(substring,obj.toArray());
    }


    @Override
    public int insert(Collect collect) throws SQLException {
        String sql ="insert into collect(collect_id,member_id,song_list_id,song_id,collect_type,is_deleted,create_time) values(?,?,?,?,?,?,?) ";
        return  JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{collect.getCollectId(),
                collect.getMemberId(),collect.getSongListId(),collect.getSongId(),collect.getCollectType()
                ,0,new Date()


        });
//        String sql="insert into collect (collectId,memberId,songListId,songId,collectType,isDeleted,createTime,updateTime) values (?,?,?,?,?,?,?,?)";
//        Object[] values=new Object[]{collect.getCollectId(),collect.getMemberId(),collect.getSongListId(),collect.getSongId(),
//                                    collect.getCollectType(),collect.getIsDeleted(),collect.getCreateTime(),collect.getUpdateTime()};
//        return JdbcUtil.insertOrUpdateOrDelete(sql,values);
    }

    /**
     * 查询所有的收藏记录
     * @param collect
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InstantiationException
     */
    @Override
    public List<Collect> queryData(Collect collect) throws IllegalAccessException, SQLException, InstantiationException {

        String sql = "select * from collect where collect_id = ? ";

        return  JdbcUtil.queryData(sql, Collect.class, new Object[]{collect.getCollectId()});
//        List<Collect> collects = JdbcUtil.queryData(sql, Collect.class);
//        return collects;

    }

    @Override
    public int update(Collect collect) throws SQLException {

        String sql="update collect set member_id = ?,song_list_id = ?,song_id = ?,collect_type = ?,is_deleted = ?,update_time = ? where collect_id=?";
        return JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                collect.getMemberId(),collect.getSongListId(),collect.getSongId(),collect.getCollectType(),collect.getIsDeleted()
                ,new Date(),collect.getCollectId()});
//        Object[] values=new Object[]{collect.getCollectId(),collect.getMemberId(),collect.getSongListId(),collect.getSongId(),
//                collect.getCollectType(),collect.getIsDeleted(),collect.getCreateTime(),new Date()};
//        return JdbcUtil.insertOrUpdateOrDelete(sql,values);

    }

    @Override
    public int deleteById(Object id) throws SQLException {
        String sql = "update collect set is_deleted = 1 ,update_time = ? where collect_id = ?";

        return JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{new Date(),id});
    }

    @Override
    public PageUtil<Collect> queryDataBypage(int currentPgae, int pageSize, Collect collect) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "select * from collect where 1=1 and is_deleted=0 ";
        ArrayList values = new ArrayList();
        if (collect.getCollectId()!=null && !"".equals(collect.getCollectId())){
            sql +="and collect_id like ? ";
            values.add("%"+collect.getCollectId()+"%");
        }

        if (collect.getSongId()!=null && !"".equals(collect.getSongId())){
            sql +="and song_id like ? ";
            values.add("%"+collect.getSongId()+"%");
        }

        if (collect.getSongListId()!=null && !"".equals(collect.getSongListId())){
            sql +="and song_list_id like ? ";
            values.add("%"+collect.getSongListId()+"%");
        }

        if (collect.getCollectType()!=null && !"".equals(collect.getCollectType())){
            sql +="and collect_type like ? ";
            values.add("%"+collect.getCollectType()+"%");
        }

//        if (collect.getCreateTime()!=null && !"".equals(collect.getCreateTime())){
//            sql +="and create_time like ? ";
//            values.add("%"+collect.getCreateTime()+"%");
//        }
//
//        if (collect.getUpdateTime()!=null && !"".equals(collect.getUpdateTime())){
//            sql +="and update_time like ? ";
//            values.add("%"+collect.getUpdateTime()+"%");
//        }

        return JdbcUtil.queryDataByPage(sql, Collect.class,currentPgae,pageSize,values.toArray());
    }
}
